﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;

namespace CamposInventory
{
    public partial class frmUsermanager : Form
    {
        FrmMain parent;
        DatabaseConnection dbconn = new DatabaseConnection();
        DataTable tbl = new DataTable();
        
        int i = 0;
       
        public frmUsermanager(FrmMain p)
        {
            InitializeComponent();
            this.Load += new EventHandler(frmUsermanager_Load);
            btnAdd.Click += new EventHandler(btnAdd_Click);
            btnupdate.Click += new EventHandler(btnupdate_Click);
            btnsearch.Click += new EventHandler(btnsearch_Click);
            btndelete.Click += new EventHandler(btndelete_Click);
            txtSearch.Click += new EventHandler(txtSearch_Click);
            btnuser.Click += new EventHandler(btnuser_Click);
            btnadmin.Click += new EventHandler(btnadmin_Click);
            
            parent = p;
        }

        void btnadmin_Click(object sender, EventArgs e)
        {
            dbconn.dbconnection();
            dbconn.loadData("Select distinct Name,Username,Level,DateCreated from tbluser where Level ='Admin' ");
            dataGridView1.DataSource = dbconn.tbl;
            updateclear();
            dbconn.DBClose();
        }

        void btnuser_Click(object sender, EventArgs e)
        {
            dbconn.dbconnection();
            dbconn.loadData("Select distinct Name,Username,Level,DateCreated from tbluser where Level ='User' ");
            dataGridView1.DataSource = dbconn.tbl;
            updateclear();
            dbconn.DBClose();
            
        }

      
        void txtSearch_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }

      
        void btndelete_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Are you sure to Delete this account ? ", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                dbconn.dbconnection();
                dbconn.UpdatePassword("DELETE  FROM tbluser WHERE Name ='" + txtSearch.Text + "'");
                MessageBox.Show("Account Deleted","Sucessfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DataView dw = new DataView(dbconn.tbl);
                dw.RowFilter = string.Format("Name LIKE '%{0}'", txtSearch.Text);
                dataGridView1.DataSource = dw;
                updateclear();
                dbconn.DBClose();     
            }
            else
            {
                this.Refresh();

            }
            
        }

        void btnupdate_Click(object sender, EventArgs e)
        {
            if (txtupname.Text != "" && txtUpusername.Text != "" && txtUppassword.Text != "" && cmblevel.Text!= "" )
            {
                dbconn.dbconnection();
                dbconn.UpdatePassword("Update tbluser set Name='" + this.txtupname.Text + "', Username ='" + this.txtUpusername.Text + "',Password ='" + this.txtUppassword.Text + "',Level = '" + this.cmblevel.Text +  "' Where Name='" + this.txtSearch.Text + "';");
                MessageBox.Show("Update success!");
                dbconn.loadData("Select distinct Name,Username,Level,DateCreated from tbluser");
                DataView dw = new DataView(dbconn.tbl);
                dw.RowFilter = string.Format("Username LIKE '%{0}'", txtSearch.Text);
                dataGridView1.DataSource = dw;
                updateclear();
                dbconn.DBClose();  
            }
            else
            {
                MessageBox.Show("Please check the information to update!", "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        void btnsearch_Click(object sender, EventArgs e)
        {

            if (txtSearch.Text == "")
            {
                MessageBox.Show("No Name has been Input", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            dbconn.dbconnection();
            dbconn.loadData("SELECT * FROM tbluser WHERE Name LIKE '%" + txtSearch.Text + "%'");
            if (dbconn.tbl.Rows.Count > 0)
            {
                tbl = dbconn.tbl;
                txtupname.Text = tbl.Rows[i][1].ToString();
                txtUpusername.Text = tbl.Rows[i][2].ToString();
                txtUppassword.Text = tbl.Rows[i][3].ToString();
                cmblevel.Text = tbl.Rows[i][4].ToString();
                dbconn.loadData("Select distinct Name,Username,level,DateCreated from tbluser where Name LIKE '%"+txtSearch.Text+"%'");
                dataGridView1.DataSource = dbconn.tbl;
            }
            else
            {
                MessageBox.Show("No user has been Found", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);      

            }
            dbconn.DBClose();
        }
        void updateclear()
        {
            txtSearch.Clear();
            txtupname.Clear();
            txtUpusername.Clear();
            txtUppassword.Clear();
            cmblevel.Text = "";
        
        }
        
        void btnAdd_Click(object sender, EventArgs e)
        {
           

                if (optAdmin.Checked && txtpass.Text == txtconfirmpass.Text && txtname.Text != "" && txtuser.Text != "" && txtpass.Text != "" )
                {
                    dbconn.dbconnection();
                    dbconn.AddData("INSERT INTO tbluser(Name,Username,Password,Level)" + "Values('" + txtname.Text + "','" + txtuser.Text + "','" + txtpass.Text + "','" + optAdmin.Text + "')");
                    
                    MessageBox.Show("User added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dbconn.DBClose();
                }
                else if (optguest.Checked  && txtpass.Text == txtconfirmpass.Text && txtname.Text != "" && txtuser.Text != "" && txtpass.Text != "" )
                {
                    dbconn.dbconnection();
                    dbconn.AddData("INSERT INTO tbluser(Name,Username,Password,Level)" + "Values('" + txtname.Text + "','" + txtuser.Text + "','" + txtpass.Text + "','" + optguest.Text + "')"); 
                    cleartextboxadd();
                    MessageBox.Show("User added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dbconn.DBClose();
                }
                else
                {
                    MessageBox.Show("Please fill up above information", "Information Missing", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            
           
        }
        void cleartextboxadd()
        {
            txtname.Text = "";
            txtuser.Text = "";
            txtpass.Text = "";
            txtconfirmpass.Text = "";
        }

        void frmUsermanager_Load(object sender, EventArgs e)
        {
           
          
        }
        
    }
}
